<template>
    <div>
        <!--        <van-tabs v-model="active" @click="clickTab" class="new-list" :swipe-threshold="5">-->
        <!--            <van-tab title="全部"></van-tab>-->
        <!--            <van-tab title="待付款"></van-tab>-->
        <!--            <van-tab title="待发货"></van-tab>-->
        <!--            <van-tab title="待收货"></van-tab>-->
        <!--            <van-tab title="已收货"></van-tab>-->
        <!--        </van-tabs>-->
        <div class="nav-t">
            <div class="nav-i" v-for="(item,index) in navList" :class="active == index?'choose':''" :key="index"
                 @click="clickTab(index)">
                <svg class="icon" aria-hidden="true">
                    <use :xlink:href="item.icon"></use>
                </svg>
                <p>{{item.title}}</p>
            </div>
        </div>
        <div class="order-list" v-if="!stateOver">
            <div v-for="(good, key) in orderList" :key="key" class="cell">
                <div @click="toDetail(good.id, good.status)">
                    <div class="top">
                        <div class="number">
                            <svg class="icon lq" aria-hidden="true" v-if="good.type==='2'">
                                <use xlink:href="#icon-user-lq"></use>
                            </svg>
                            订单号：{{good.id}}
                        </div>
                        <div>{{good.status | orderState}}</div>
                    </div>
                    <div class="g-body">
                        <order-goods :orderGoods="good.products" :type="type"></order-goods>
                    </div>
                    <div class="total">
                        <div>数量：{{good.num}}</div>
                        <div>合计：{{good.money | moneyFmt(2, '￥')}}</div>
                    </div>
                    <div class="line"></div>
                </div>
                <order-btn :good="good" type="list" @finishCancel="finish" v-if="!stateOver"/>
                <div class="btn-body" v-else-if="good.process===refundTypes.CREATE">
                    <div class="btn1">
                        <cancelRefund class="btn" :refundId="good.id"/>
                    </div>
                </div>
            </div>
            <infinite-loading :identifier="infiniteId"
                              @infinite="infiniteHandler"
                              spinner="circles"
            >
                <div class="no-more" slot="no-more">没有更多了</div>
                <div class="no-more" slot="no-results">暂时没有相关订单</div>
            </infinite-loading>
        </div>

        <div class="order-list" v-else-if="stateOver">
            <div v-for="(good, key) in orderList" :key="key" class="cell">
                <div @click="toDetail2(good.id, good.process)">
                    <div class="top">
                        <div class="number">
                            <svg class="icon lq" aria-hidden="true" v-if="good.type==='2'">
                                <use xlink:href="#icon-user-lq"></use>
                            </svg>
                            订单号：{{good.oid}}
                        </div>
                        <div>{{good.process | refundStatus}}</div>
                    </div>
                    <div class="g-body">
                        <order-goods :orderGoods="good.products" :type="type"></order-goods>
                    </div>
                    <div class="total">
                        <div>数量：{{good.products[0].num}}</div>
                        <div>合计：{{good.money}}</div>
                    </div>
                    <div class="line"></div>
                </div>
                <div class="btn-body" v-if="good.process===refundTypes.CREATE">
                    <div class="btn1">
                        <cancelRefund class="btn" :refundId="good.id"/>
                    </div>
                </div>
            </div>
            <infinite-loading :identifier="infiniteId"
                              @infinite="infiniteHandler"
                              spinner="circles"
            >
                <div class="no-more" slot="no-more">没有更多了</div>
                <div class="no-more" slot="no-results">暂时没有相关订单</div>
            </infinite-loading>
        </div>
    </div>
</template>

<script>
    import API from '../../../request/api'
    import OrderBtn from "../../../components/User/orderbtn";
    import OrderGoods from "../../../components/User/order-goods";
    import RefundTypes from "../../../common/RefundTypes";
    import cancelRefund from "../../../components/User/cancelRefund";
    import {commonMixin} from '@/common/mixin';

    export default {
        components: {
            OrderGoods,
            OrderBtn,
            RefundTypes,
            cancelRefund
        },
        mixins: [commonMixin],
        data() {
            return {
                refundTypes: RefundTypes.types, //售后进度
                pageIndex: 1,
                active: 0,
                infiniteId: +new Date(), // 该值改变后重新调用handler方法
                loading: false,
                finished: [],
                pages: [],
                orderList: [],
                type: 1,
                stateOver: false,
                system: this.$store.state.login.system,
                navList: [
                    {
                        icon: '#icon-dingdan',
                        title: '全部'
                    }, {
                        icon: '#icon-send1',
                        title: '待付款'
                    }, {
                        icon: '#icon-receive1',
                        title: '待发货'
                    }, {
                        icon: '#icon-received1',
                        title: '待收货'
                    }, {
                        icon: '#icon-couponcopy1',
                        title: '已收货'
                    }, {
                        icon: '#icon-couponcopy1',
                        title: '售后'
                    },
                ]
            };
        },
        computed: {
            status() {
                return this.active === 0 ? 10 : this.active - 1;
            }
        },
        created() {
            this.active = parseInt(this.$route.query.active);
            this.showGlobaLoading(); //打开loading
        },
        watch: {
            active() {
                this.pageIndex = 1;
                this.orderList = [];
                this.infiniteId += 1;
                this.showGlobaLoading(); //打开loading
            }
        },
        methods: {
            toDetail2(id, process) {
                if(process===this.refundTypes.AGREE){
                    // 跳转至商家填写物流信息
                    this.$router.push({
                        name: 'refund-wait-return',
                        query: {
                            refundId: id
                        }
                    });
                }else{
                    // 跳转至售后订单详情页
                    this.$router.push({
                        name: 'refund-state',
                        query: {
                            refundId: id
                        }
                    });
                }
            },
            finish(id) {
                // this.orderList.filter((item,index)=>{
                //     if(item.id === id){
                //         this.orderList.splice(index,1)
                //     }
                // })
                // this.orderList.filter((item,index)=>{
                //     if(item.id === id){
                //         this.orderList.splice(index,1)
                //     }
                // })
                this.$toast({
                    message: '取消成功',
                    duration: 1000
                });
                setTimeout(() => {
                    location.reload()
                }, 1000)
            },
            clickTab(index) {
                this.active = index;
                console.log(this.active, '1')
                if (this.active == 5) {
                    this.stateOver = true
                } else {
                    this.stateOver = false
                }
            },
            toDetail(val, type) {
                this.$router.push({
                    name: 'orderDetail',
                    query: {
                        ID: val,
                        type: type
                    }
                });
            },
            infiniteHandler($state) {
                console.log('111111111111111')
                console.log(this.stateOver)
                setTimeout(() => {
                    if (this.stateOver) {
                        //     售后订单
                        this.request.get(API.afterlist, {
                            type: 1,
                            page: this.pageIndex
                        }).then((res) => {
                            if (res.list.length) {
                                this.orderList.push(...res.list);
                                if (res.list.length < 20) {
                                    $state.complete();
                                }
                                this.pageIndex += 1;
                                $state.loaded();
                            } else {
                                $state.complete();
                            }
                            this.hideGlobaLoading(); //关闭loading
                        }).catch((err) => {
                            console.error('orderList-err', err);
                        });
                    } else {
                        // 其余订单
                        this.request.get(API.orderList, {
                            status: this.status,
                            type: 1,
                            page: this.pageIndex
                        }).then((res) => {
                            if (res.list.length) {
                                this.orderList.push(...res.list);
                                if (res.list.length < 20) {
                                    $state.complete();
                                }
                                this.pageIndex += 1;
                                $state.loaded();
                            } else {
                                $state.complete();
                            }
                            this.hideGlobaLoading(); //关闭loading
                        }).catch((err) => {
                            console.error('orderList-err', err);
                        });
                    }
                }, 500)
            },
            onLoad() {
                if (this.finished[this.active] === false) {
                    console.log(this.active);
                    // 异步更新数据
                    let data = {
                        status: this.status,
                        type: 1,
                        page: this.pages[this.active]
                    };
                    //     // this.receive = true;
                    console.log('2222222222222222')
                    this.request.get(API.orderList, data).then(res => {
                        this.pages[this.active]++;
                        console.log('this.pages[this.active]++;', this.pages[this.active]++)
                        let newData = this.orderList[this.active].concat(res.list);
                        this.$set(this.orderList, this.active, newData);
                        if (res.total_page < this.pages[this.active]) {
                            this.finished[this.active] = true;
                        }
                        this.loading = false;
                    }, () => {
                    });
                }
            }
        },
        beforeCreate() {
            // 可以在这加个loading事件，在加载实例时触发
            // 这个阶段实例的data、methods是读不到的
        },
        beforeMount() {
            // 相关的 render 函数首次被调用。
        },
        mounted() {
            // 挂载元素，获取到DOM节点
            // el选项的DOM节点 被新创建的 vm的el 替换，并挂载到实例上去之后调用此生命周期函数。此时实例的数据在DOM节点上进行渲染
        },
        beforeUpdate() {
            // 数据更新时调用，但不进行DOM重新渲染，在数据更新时DOM没渲染前可以在这个生命函数里进行状态处理
        },
        updated() {
            //  如果对数据统一处理，在这里写上相应函数
            // 这个状态下数据更新并且DOM重新渲染，当这个生命周期函数被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。当实例每次进行数据更新时updated都会执行
        },
        beforeDestroy() {
            // 可以做一个确认停止事件的确认框
        },
        destroyed() {
            // Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，所有的事件监听器会被移除，所有的子实例也会被销毁
        }
    };

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    @import '~assets/main.styl'
    .nav-t {
        flex-x(, stretch)
        padding 5vw 0
        height vw(70)
        box-sizing border-box
        background grayF

        .nav-i {
            flex-y()
            flex 1

            .icon {
                width vw(17)
                height vw(17)
                fill gray6

            }

            p {
                word-vw(12px, gray6)
            }
        }

        .choose {
            .icon {
                fill main-color
            }

            p {
                color main-color
            }
        }
    }

    .new-list {
        position fixed
        z-index 99
        top 0
        width 100%
    }

    .order-list {
        .btn-body {
            display flex
            justify-content space-between
            align-items center
            padding 0 cellPadding

            .btn1 {
                display flex
                justify-content flex-end
                flex 1

                .btn2 {
                    btn-gray()
                }

                .btn {
                    btn-main()
                }

                div {
                    margin-left middleMargin
                }

            }
        }
    }

    .cell {
        margin-top vw(10)
        background-color grayF
        padding 0

        .top {
            display flex
            align-items center
            justify-content space-between
            font-size word12
            color main-color
            padding cellPadding cellPadding 0

            .number {
                display flex
                align-items center
                color gray3

                .lq {
                    width vw(24)
                    height vw(24)
                    fill #F8BE43
                    margin-right titleMargin
                }
            }
        }

        .total {
            display flex
            align-items center
            justify-content space-between
            padding 0 cellPadding
            line-height 3
        }

        .line {
            width 100vw
            height vw(1)
            background-color #f8f8f8
        }
    }

    .no-more {
        text-align center
        color gray9
        line-height vw(30)
        margin-top smallMargin
    }
</style>
